package com.zy.utils;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.zy.utils.data.Point;
import com.zy.zlog.ZLog;

import java.util.ArrayList;
import java.util.List;

//画布工具
public class CanvasUtils {


    //画一个数组，从中心向两边开始绘制
    public static Point[] drawArray1(int[] array, int baseX, int baseY, int w, int h) {
        int length = array.length;
        Point[] points = new Point[length];
        if (length % 2 == 0) { //偶数个元素
            drawArray1(points, array, baseX + w + 5, baseY, w, true, length / 2, length);
            drawArray1(points, array, baseX - w - 5, baseY, w, false, 0, length / 2 - 1);
        } else { //奇数个元素
            //画中间元素
//            canvas.drawRect(baseX - w, baseY - h, baseX + w, baseY + h, backPaint);
//            drawTextCenter(canvas, paint, baseX, baseY, "" + array[length / 2]);
            points[length / 2] = new Point(baseX, baseY, "2", 50, 0);
            drawArray1(points, array, baseX + 2 * w + 10, baseY, w, true, length / 2 + 1, length);
            drawArray1(points, array, baseX - 2 * w - 10, baseY, w, false, 0, length / 2 - 1);
        }

        return points;
    }


    //画一个数组，从中心向两边开始绘制
    public static void drawArray(Canvas canvas, int[] array, Paint paint, Paint backPaint, int baseX, int baseY, int w, int h) {
        int length = array.length;
        if (length % 2 == 0) { //偶数个元素
            drawArray(canvas, array, paint, backPaint, baseX + w + 5, baseY, w, h, true, length / 2, length);
            drawArray(canvas, array, paint, backPaint, baseX - w - 5, baseY, w, h, false, 0, length / 2 - 1);
        } else { //奇数个元素
            //画中间元素
            canvas.drawRect(baseX - w, baseY - h, baseX + w, baseY + h, backPaint);
            drawTextCenter(canvas, paint, baseX, baseY, "" + array[length / 2]);
            drawArray(canvas, array, paint, backPaint, baseX + 2 * w + 10, baseY, w, h, true, length / 2 + 1, length);
            drawArray(canvas, array, paint, backPaint, baseX - 2 * w - 10, baseY, w, h, false, 0, length / 2 - 1);
        }
    }

    //分组绘制
    private static void drawArray1(Point[] points, int[] array, int baseX, int baseY, int w, boolean up, int start, int end) {
        ZLog.d("CanvasUtils", "drawArray start:" + start + " end:" + end);
        if (up) {
            for (int i = start; i < end; i++) {
                int x = baseX + (i - start) * 2 * (w + 10);
                points[i] = new Point(x, baseY, "55", 50, 0);
            }
        } else {
            for (int i = end; i >= 0; i--) {
                int x = baseX - (end - i) * 2 * (w + 10);
                points[i] = new Point(x, baseY, "22", 50, 0);
            }
        }
    }

    //分组绘制
    private static void drawArray(Canvas canvas, int[] array, Paint paint, Paint backPaint, int baseX, int baseY, int w, int h, boolean up, int start, int end) {
        ZLog.d("CanvasUtils", "drawArray start:" + start + " end:" + end);
        if (up) {
            for (int i = start; i < end; i++) {
                int x = baseX + (i - start) * 2 * (w + 10);
                ZLog.d("CanvasUtils", "drawArray x:" + x);
                canvas.drawRect(x - w, baseY - h, x + w, baseY + h, backPaint);
                drawTextCenter(canvas, paint, x, baseY, "" + array[i]);
            }
        } else {
            for (int i = end; i >= 0; i--) {
                int x = baseX - (end - i) * 2 * (w + 10);

                canvas.drawRect(x - w, baseY - h, x + w, baseY + h, backPaint);
                drawTextCenter(canvas, paint, x, baseY, "" + array[i]);
            }
        }
    }

    public static void drawTextCenter(Canvas canvas, Paint paint, float centerX, float centerY, String text) {
        //计算基线坐标
        float baseLineY = getBaseLineY(paint, centerY);
        canvas.drawText(text, centerX, baseLineY, paint);
    }


    private static float getBaseLineY(Paint textPaint, float centerY) {
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        return centerY + (fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;
    }


    //绘制横竖辅助线
    public static void drawAssistLine(Canvas canvas, Paint assistLinePaint, int d) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();

        int baseX = d;
        int baseY = d;

        while (baseX < w) {
            canvas.drawLine(baseX, 0, baseX, h, assistLinePaint);
            baseX += d;
        }

        while (baseY < h) {
            canvas.drawLine(0, baseY, w, baseY, assistLinePaint);
            baseY += d;
        }
    }

    //绘制横竖辅助线
    public static void drawAssistLineV2(Canvas canvas, Paint assistLinePaint, int d) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();


        int wSize = w / d;
        int wAss = wSize / 2 * d + d / 2;

        canvas.drawLine(wAss, 0, wAss, h, assistLinePaint);


        int hSize = h / d;
        int hAss = hSize / 2 * d + d / 2;

        canvas.drawLine(0, hAss, w, hAss, assistLinePaint);
    }


    public static void drawAssistLineY(Canvas canvas, Paint assistLinePaint, int d) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        int baseY = d;
        while (baseY < h) {
            canvas.drawLine(0, baseY, w, baseY, assistLinePaint);
            baseY += d;
        }
    }

    public static void drawAssistLineX(Canvas canvas, Paint assistLinePaint, int d) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        int baseX = d;
        while (baseX < w) {
            canvas.drawLine(baseX, 0, baseX, h, assistLinePaint);
            baseX += d;
        }
    }


    /**
     * @param baseX
     * @param baseY
     * @param d     间距
     * @param array 数组
     * @return
     */
    public static List<Point> buildFirst(int baseX, int baseY, int d, int[] array, int per, Point.Type type) {
        ArrayList<Point> list = new ArrayList<>();

        for (int i = 0; i < array.length; i++) {

            Point point = new Point.Builder().setOriginX(baseX + d * i)
                    .setOriginY(baseY)
                    .setV(String.valueOf(array[i]))
                    .setPer(per)
                    .setIndex(i)
                    .build(type);
            list.add(point);
//            if (type == 4) {
//
//            } else {
//                list.add(new Point(baseX + d * i, baseY, String.valueOf(array[i]), per, i));
//            }
        }

        return list;
    }


    public static List<? extends Point> buildAssistList(int baseX, int baseY, int d, int[] array, int per, Point.Type type) {
        ArrayList<Point> list = new ArrayList<>();

        for (int i = 0; i < array.length; i++) {
            Point point = new Point.Builder().setOriginX(baseX + d * i)
                    .setOriginY(baseY)
                    .setV(String.valueOf(array[i]))
                    .setPer(per)
                    .setIndex(i)
                    .build(type);
            list.add(point);
//            if (type == 1) {
//                list.add(new AssistPoint(baseX + d * i, baseY, String.valueOf(array[i]), per));
//            }
        }

        return list;
    }
}
